function G=newgame(players,varargin)
% newgame: Create a new game object
%
% SYNTAX:
%   G = newgame({player1,player2,...},,prop1,value1,...)
%   Return generated MADS.Game object G with specified players and properties
%   Each player should be created with function NEWPLAYER.
%   Type "newgame({})" to see available properties and their default values. 
%   You should define all game-specific (rather than player-specific) parameters or properties with this function.
%   Properties are not restricted in predefined properties.
%   Once a game object G is created, you can use SIMGAME(G) to simulate it.
%
% EXAMPLE:
%
%   P1=newplayer('pos',randn(1,2),'motion','random','speed',10,'legend','r')
%   P2=newplayer('pos',randn(1,2),'motion','random','speed',2,'legend','b')
%   G=newgame({P1,P2},'max_step',100,'pause','pause;')
%   G=simgame(G)
%
%  The above example simulates a game with two players with random motion.
%  In this game, players have random initial position, different speeds and colors. 
%  More interesting examples can be seen by running DEMO_MADS.M or EX_*.M.
%   
% See also NEWPLAYER, SIMGAME

%   Author: Hongbin Ma
%   Last updated: 2009-6-2 with MATLAB 6.5
%   Problems or suggestions? Email me: mathmhb[at]163.com

G=struct('type','MADS.Game',varargin{:});
G=set_field(G,'orders',[]);
G=set_field(G,'max_time',100);
G=set_field(G,'max_step',10000);
G=set_field(G,'min_r',0);
G=set_field(G,'h',1);
G=set_field(G,'t0',0);
G=set_field(G,'tf',10);
G=set_field(G,'draw',1);
G=set_field(G,'figure',1);
G=set_field(G,'figure2',2);
G=set_field(G,'ontop',1);
G=set_field(G,'ontop2',1);
G=set_field(G,'axis','');
G=set_field(G,'axis2','');
G=set_field(G,'grid','on');
G=set_field(G,'grid2','off');
G=set_field(G,'legend','');
G=set_field(G,'legend2','');
G=set_field(G,'screen_size',get_screen_size);
G=set_field(G,'window_size',G.screen_size/2-5);
G=set_field(G,'window_pos',[0,G.screen_size(2)/2-70]);
G=set_field(G,'title','');
G=set_field(G,'title2','');

G=set_field(G,'moviefile','');
G=set_field(G,'movie',[]);
if isempty(G.movie) & ~isempty(G.moviefile),
  G.movie=newmovie(G.moviefile);
end;

G=set_field(G,'snapshotfile','');
G=set_field(G,'snapshot',[]);
if isempty(G.snapshot) & ~isempty(G.snapshotfile),
  G.snapshot=newsnapshot(G.snapshotfile);
end;

G=set_field(G,'obstacles',{});
G=set_field(G,'boundary',[]);

G=set_field(G,'plot_components',[]);
G=set_field(G,'plot_xlabel','time step');
G=set_field(G,'plot_labels',{});
G=set_field(G,'plot_titles',{});
for j=G.plot_components(:)',
    if j>length(G.plot_labels),
        G.plot_labels{j}=['Component [',num2str(j),']'];
    end;
    if j>length(G.plot_titles),
        G.plot_titles{j}=['Component [',num2str(j),']'];
    end;    
end;

G=set_field(G,'figure_file','');
G=set_field(G,'figure2_file','');

G=set_field(G,'pause','pause');
G=set_field(G,'draw_game_init_func','def_draw_game_init');
G=set_field(G,'draw_game_board_func','def_draw_game_board');
G=set_field(G,'draw_game_finish_func','def_draw_game_finish');
G=set_field(G,'is_gameover_func','def_is_gameover');
G=set_field(G,'after_draw_func','def_after_draw_game');
G=set_field(G,'before_draw_func','def_before_draw_game');
G=set_field(G,'draw_game_trace_func','def_draw_game_trace');
if (length(G.axis)==6),
  G.wz=G.axis(6)-G.axis(5);
end;

G.P=players;
G.N=length(players);

G.t=G.t0;
G.step=0;
if G.N==2,
  G.r=norm(G.P{1}.pos-G.P{2}.pos);
else,
  G.r=inf;
end;

G.Tdata=[G.t0];
for k=1:G.N;
    G.P{k}.last_pos=G.P{k}.pos;
    G.P{k}.last_ang=G.P{k}.ang;
end;
